package cn.lg.soar.core.propertie.security;

import eu.bitwalker.useragentutils.DeviceType;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Lazy;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author luguoxiang
 * @date 2022/4/4
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Lazy
@ConfigurationProperties(value = "lg.security.refresh-token")
public class RefreshTokenProperties {

    /**
     * 有效期
     */
    private Duration expire;

    /**
     * 密钥
     */
    private String secret;

    /**
     * 按照设备类型设置有效期
     */
    private Map<DeviceType, Duration> deviceExpire = new HashMap<>(16);

    public Duration getExpire() {
        if (expire == null) {
            expire = Duration.ofDays(7);
        }
        return expire;
    }

    public void setExpire(Duration expire) {
        this.expire = expire;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public Map<DeviceType, Duration> getDeviceExpire() {
        return deviceExpire;
    }

    public void setDeviceExpire(Map<DeviceType, Duration> deviceExpire) {
        this.deviceExpire = deviceExpire;
    }

    public Duration getExpire(DeviceType deviceType) {
        return deviceExpire.computeIfAbsent(deviceType, k -> getExpire());
    }

    public long getExpireMillis(DeviceType deviceType) {
        return getExpire(deviceType).getSeconds() * 1000;
    }
}
